<?php
class UploadAction extends AdminAction{

	/**
	 * ajax图片上传，用于uploadify等js插件
	 * @param  $_POST['type'] int 1返回小图，2返回中图，3返回大图
	**/
	public function ajax(){

 		if(!isset($_POST[session_name()])){
 			exit;
 		}else{
 			session_id($_POST[session_name()]);
 			session_start();
 		}
 
 		if(isset($_FILES)){$res=self::_upload();}

		if(is_array($res)){
			if($_POST['type']==1){
				$temp=explode('/',$res['savename']);$temp[count($temp)-1]='s_'.$temp[count($temp)-1];
				echo implode('/',$temp);
			}
			if($_POST['type']==2){
				$temp=explode('/',$res['savename']);$temp[count($temp)-1]='m_'.$temp[count($temp)-1];	
				echo implode('/',$temp);
			}
			if($_POST['type']==3){echo $res['savename'];}		
		}
		
	}

	/*
	 * CKEDITOR编辑器图片上传
	 */
	public function ckupload(){

		if(isset($_FILES)){$res=self::_upload();}else{return;}

		$server=C('TMPL_PARSE_STRING.__PICSERVER1__');		//图片服务器
		$file_path=C('TMPL_PARSE_STRING.__UPLOAD__');		//图片保存目录
		$list=C('LIST');									//网站目录

		if(is_array($res)){
			$message="上传成功";
			if(empty($server)){
				$fileurl=$list.$file_path.'/'.$res['savename'];	
			}else{
				$fileurl=$server.$list.$file_path.'/'.$res['savename'];
			}
				
			$str='<script type="text/javascript">window.parent.CKEDITOR.tools.callFunction('.$_GET['CKEditorFuncNum'].', \''.$fileurl.'\', \''.$message.'\');</script>';	
		}else{
			$message="上传失败";
			$str='<script type="text/javascript">window.parent.CKEDITOR.tools.callFunction('.$_GET['CKEditorFuncNum'].', \''.$fileurl.'\', \''.$message.'\');</script>';
		}

		echo $str;
	}

	/**
 	 *文件上传类参数说明：
 	 * @param string $data['type']		允许上传的文件类型，默认为'jpg,gif,png,jpeg'
 	 * @param int    $data['maxsize'] 	允许上传的文件大小，默认3M
	 * @param boole	 $data['thumb']		是否生成缩略图，默认为否
	 * @param string $data['prefix']    缩略图前缀
	 * @param int    $data['height']    缩略图高度
	 * @param int    $data['width']     缩略图宽度
	 * @param boole	 $data['water']		是否生成水印，默认为否
	 * @param int 	 $data['alpha']     水印透明度
	 * @param string $data['waterfile'] 水印文件路径
	**/
	protected function _upload($data=array('type'=>'jpg,gif,png,jpeg','maxsize'=>3,'thumb'=>true,'prefix'=>'m_,s_','width'=>'350,120','height'=>'450,150','water'=>false,'alpha'=>80,'waterfile'=>'./Public/water/water.png',)) {

		//默认配置参数
		$type=explode(',',$data['type']);
		$maxsize=$data['maxsize']*1024000;	//将图片大小限制单位转换为M
		$thumb=$data['thumb'];
		$prefix=$data['prefix'];
		$width=$data['width'];
		$height=$data['height'];
		$water=$data['water'];
		$alpha=$data['alpha'];
		$waterfile=$data['waterfile'];
		//默认配置参数

		//实例化上传类
		import('ORG.Net.UploadFile');
		$upload = new UploadFile();

		$upload->maxSize  			= $maxsize ;					// 设置附件上传大小
		$upload->allowExts  		= $type;			// 设置附件上传类型
		//设置附件上传目录
		$upload->savePath 			='.'.C('TMPL_PARSE_STRING.__UPLOAD__').'/';	
		//设置需要生成缩略图，仅对图像文件有效
	    $upload->thumb            	= $thumb;		
	    $upload->autoSub			= true;					//开启子目录保存
	    $upload->subType			= date;					//子目录创建方式为日期
	    $upload->hashLevel			= 2;					//子目录层级
	    //设置需要生成缩略图的文件后缀
	    $upload->thumbPrefix        = $prefix;  			//缩略图前缀
	    $upload->thumbMaxWidth      = $width;       		//设置缩略图最大宽度
	    $upload->thumbMaxHeight     = $height;     		    //设置缩略图最大高度
	    $upload->saveRule           = 'uniqid';        		//设置上传文件规则
	    $upload->thumbRemoveOrigin  = false ;        		//删除原图
	        
		if(!$upload->upload()) {
				// 上传错误提示错误信息
			   return $upload->getErrorMsg();
		}else{	// 上传成功
			if(!$water){
				$info=$upload->getUploadFileInfo();
				$info[0]['path']=$upload->savePath;
				return $info[0];
			}else{
				$info=$upload->getUploadFileInfo();
				import('ORG.Util.Image');	//加载缩略图类
				$Image = new Image();
				$Image->water($info[0]['savepath'].$info[0]['savename'],$waterfile,'',$alpha);
				$Image->water($info[0]['savepath'].'m_'.$info[0]['savename'],$waterfile,'',$alpha);

				return $info[0];

			}
		}
	}

}